﻿//API.cs   文件，定义一些常用API函数及常量     

using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace myking
{
    ///   <summary>     
    ///   API   的摘要说明。     
    ///   </summary>     
    public static class API
    {
        public static int WM_KEYDOWN = 0x0100;
        public static int WM_KEYUP = 0x0101;
        public static int WM_SYSKEYDOWN = 0x0104;
        public static int WM_SYSKEYUP = 0x0105;

        public static int WM_MOUSEMOVE = 0x0200;
        //public static int WM_LBUTTONDOWN = 0x0201;
        //public static int WM_LBUTTONUP = 0x0202;

        //按下鼠标左键
        public static uint WM_LBUTTONDOWN = 0x201;
        //弹起鼠标左键
        public static uint WM_LBUTTONUP = 0x202;

        public static int WM_LBUTTONDBLCLK = 0x0203;
        public static int WM_RBUTTONDOWN = 0x0204;
        public static int WM_RBUTTONUP = 0x0205;
        public static int WM_RBUTTONDBLCLK = 0x0206;
        public static int WM_USER = 0x0400;

        public static int MK_LBUTTON = 0x0001;
        public static int MK_RBUTTON = 0x0002;
        public static int MK_SHIFT = 0x0004;
        public static int MK_CONTROL = 0x0008;
        public static int MK_MBUTTON = 0x0010;

        public static int MK_XBUTTON1 = 0x0020;
        public static int MK_XBUTTON2 = 0x0040;

        public static IntPtr programhWnd { get; set; }

        //[DllImport("user32.dll")]
        //public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        //[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        //static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        static extern bool PostMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    


        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
        public static extern IntPtr GetForegroundWindow();
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public static extern int GetWindowRect(IntPtr hwnd, ref System.Drawing.Rectangle lpRect);

        //此处主要用来让窗口置于最前(SetWindowPos(this.Handle,-1,0,0,0,0,0x4000|0x0001|0x0002);)     
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd,
        int hWndInsertAfter,
        int X,
        int Y,
        int cx,
        int cy,
        int uFlags
        );

        public static void mouseUpDown()
        {

            const uint BM_CLICK = 0xF5;

            PostMessage(programhWnd, BM_CLICK, 0, 0);

            int x = 277;
            int y = 70;
            //TestGetForegroundWindowRectangle(out x, out y);

            //写法1
            PostMessage(programhWnd, WM_LBUTTONDOWN, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);
            Thread.Sleep(500);
            PostMessage(programhWnd, WM_LBUTTONUP, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);

            PostMessage(programhWnd, WM_LBUTTONDOWN, 0,0);
            Thread.Sleep(500);
            PostMessage(programhWnd, WM_LBUTTONUP, 0,0);


            ////简化下  其实就是Y坐标左移16位,然后再加上X坐标
            //PostMessage(programhWnd, WM_LBUTTONDOWN, 0, x + (y << 16));
            //Thread.Sleep(500);
            //PostMessage(programhWnd, WM_LBUTTONUP, 0, x + (y << 16));


            //int x;
            //int y;
            //TestGetForegroundWindowRectangle(out x, out y);
            //x = 184; // X coordinate of the click 
            //y = 122; // Y coordinate of the click 
            //IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates 
            //IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl) 
            //const uint downCode = 0x201; // Left click down code 
            //const uint upCode = 0x202; // Left click up code 
            //PostMessage(programhWnd, downCode, wParam, lParam); // Mouse button down 
            //Thread.Sleep(300);
            //PostMessage(programhWnd, upCode, wParam, lParam); // Mouse button up 

            //int x = 60;
            //int y = 85;
            //IntPtr p = (IntPtr)((y << 16) | x);
            //IntPtr wParam = IntPtr.Zero;
            ////PostMessage(programhWnd, WM_LBUTTONDOWN, wParam, p);
            //Thread.Sleep(500);
            //PostMessage(programhWnd, WM_LBUTTONUP, wParam, p);

        }

        public static void TestGetForegroundWindowRectangle(out int x,out int y)
        {
            x = 0;
            y = 0;
            IntPtr hForeground = programhWnd;//GetForegroundWindow();
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
            GetWindowRect(hForeground, ref rect);
            x = rect.X * 65535 / rect.Width;
            y = rect.Y * 65535 / rect.Height;
        }

    }
}